package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.system.domain.IotInterfaceApiBo;
import com.lechi.web.system.mapper.iot.IotInterfaceApiMapper;
import com.lechi.web.system.service.IIotInterfaceApiService;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 接口发布管理Service业务层处理
 *
 * @author yangguang
 * @date 2025-03-31
 */
@Service
public class IotInterfaceApiServiceImpl implements IIotInterfaceApiService {

    @Autowired
    private IotInterfaceApiMapper iotInterfaceApiMapper;

    @Autowired
    private Scheduler scheduler;

    /**
     * 查询接口发布管理列表
     *
     * @param iotInterfaceApi 接口发布管理
     * @return 接口发布管理
     */
    @Override
    public List<IotInterfaceApiBo> selectIotInterfaceApiList(IotInterfaceApiBo iotInterfaceApi) {
        return iotInterfaceApiMapper.selectIotInterfaceApiList(iotInterfaceApi);
    }


    /**
     * 查询接口发布管理列表
     *
     * @param iotInterfaceApiBo 接口发布管理
     * @return 接口发布管理
     */
    @Override
    public List<IotInterfaceApiBo> selectAuthInterfaceDevice(IotInterfaceApiBo iotInterfaceApiBo) {
        return iotInterfaceApiMapper.selectAuthInterfaceDevice(iotInterfaceApiBo);
    }

    /**
     * 修改接口发布管理
     *
     * @param iotInterfaceApi 接口发布管理
     * @return 结果
     */
    @Override
    public int updateIotInterfaceApi(IotInterfaceApiBo iotInterfaceApi) {
        iotInterfaceApi.setCreateTime(new Date());
        try {
            return iotInterfaceApiMapper.updateIotInterfaceApi(iotInterfaceApi);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "修改接口配置", method, iotInterfaceApi));
            throw new ServiceException(ErrorCodeEnum.UPDATE_ERROR.getValue(), ErrorCodeEnum.UPDATE_ERROR.getKey());
        }
    }
}
